package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.IntelligentChatAdapter;
import com.zmx.patientend.adapter.InterrogationResultsAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.Interogation_message;
import com.zmx.patientend.pojo.InterrogationResult;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/11/11  23:04
 * 功 能：智能问诊结果
 */
public class InterrogationResultsActivity extends BaseActivity {

    @BindView(R.id.image_head)
    CircleImageView imageHead;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private InterrogationResultsAdapter adapter;
    private List<InterrogationResult> lists;

    private String order;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_interrogation_results;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        order = this.getIntent().getStringExtra("order");
        Log.e("发送的订单变红", "接收"+order);
        Glide.with(mActivity).load(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.avatar, "")).into(imageHead);
        time.setText("问诊时间："+simpleDateFormat.format(new Date()));
        phone.setText("患者姓名："+MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.name, ""));


        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new InterrogationResultsAdapter(mActivity);
        recyclerView.setAdapter(adapter);
        lists = new ArrayList<>();

        getInfo();


    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

        try {

            JSONObject object = new JSONObject(msg.obj.toString());
            JSONArray data = object.getJSONArray("data");

            for (int i = 0;i<data.length();i++){

                InterrogationResult ir = new InterrogationResult();
                ir.setDisease(data.getJSONObject(i).getString("disease"));
                ir.setClass_name(data.getJSONObject(i).getString("class_name"));
                JSONArray keyword = data.getJSONObject(i).getJSONArray("keyword");

                List<String> ks = new ArrayList<>();
                for (int j = 0 ;j<keyword.length();j++){
                    ks.add(keyword.get(j).toString());
                }
                ir.setKeyword(ks);


                if (!data.getJSONObject(i).isNull("list")) {

                    JSONArray ms = data.getJSONObject(i).getJSONArray("list");
                    List<Interogation_message> lims = new ArrayList<>();
                    for (int s = 0 ;s<ms.length();s++){

                        Interogation_message im = new Interogation_message();
                        im.setId(ms.getJSONObject(s).getInt("id"));
                        im.setTitle(ms.getJSONObject(s).getString("title"));
                        lims.add(im);
                    }
                    ir.setLis(lims);

                }


                lists.add(ir);

            }

            adapter.setListAndNotifyDataSetChanged(lists);

        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("UI从","仄"+e.toString());
        }
                    break;

            }

        }
    };

    public void getInfo() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("order", order);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.robot/finals", params, handler, 1, 404);

    }

}
